package CtrLayer;


import java.util.ArrayList;

import DBLayer.DBRoom;
import DBLayer.DBSession;
import DBLayer.DbConnection1;
import DBLayer.IFDBSession;
import DomainLayer.Journal;
import DomainLayer.Room;
import DomainLayer.Session;

public class SessionCtr { 
	
	private IFDBSession dbsession;
	private JournalCtr journalCtr;
	private  DBRoom dbRoom;
	
	public SessionCtr()
	{
		
		dbsession=new DBSession();
		journalCtr=new  JournalCtr();
		dbRoom= new DBRoom();
	}
	
	
	/**
	 * checks on which dates the room is occupied
	 * @param roomNumber the number of the room
	 * @return list of dates when the room is occupied
	 */
	public ArrayList<Integer> getUnoccupiedRooms( String date)
	{
	    ArrayList<Integer> roomNumbers = new ArrayList<Integer>();
		for(Room room:dbsession.getUnoccupiedRooms(date)){
			roomNumbers.add(room.getRoom_No());
		}
	    
	    return roomNumbers;
		
	}
	
	public void createSession(String date,int room_No, String teacher)
	{   dbRoom= new DBRoom();
	    Room room= dbRoom.findRoom(room_No);
		Session session=new Session(date,room,teacher);
		dbsession.createSession(session);
	}
	
	/**
	 * Finalize the creation of session and assign all the journals to their according beds
	 * @param date the date for the session
	 * @param room the room for the session
	 * @param teacher
	 * @param beds the beds to which the journal will be assigned
	 * @param journals the journals the be assigned
	 */
	public void finalizeSession(String date,int room_No,String teacher,ArrayList<String> listJournalBed)
	{
		
		DbConnection1.startTransaction();
		createSession(date,room_No,teacher);
		
		for(String s: listJournalBed)
		{
			
			String[] temp = s.split("\\.");
			int bed = Integer.parseInt(temp[0]);
			temp=temp[1].split(":");
			String journalName= temp[0];
			
			String contactCauses=temp[1];
			
			
			
			journalCtr.assignToBed(journalCtr.findJournal(journalName,contactCauses), room_No, bed, date);
			
			
		}
		
		if(!DbConnection1.commitTransaction())
			DbConnection1.rollbackTransaction();
	}
	
	
	public int getNrOfBeds(int room_No){
		Room room = dbRoom.findRoom(room_No);
		
		return room.getBeds().size();
		
	}
	
	


}
